package com.demo.project_template.service.impl;

import com.demo.project_template.entity.Friend;
import com.demo.project_template.entity.Gift;
import com.demo.project_template.entity.UserGift;
import com.demo.project_template.exception.ErrorCodeException;
import com.demo.project_template.mapper.FriendMapper;
import com.demo.project_template.mapper.GiftMapper;
import com.demo.project_template.service.FriendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.project_template.service.UserGiftService;
import com.demo.project_template.util.DateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 畅聊好友关系表 服务实现类
 * </p>
 *
 * @author 杨玉龙
 * @since 2024-08-22 14:18:23
 */
@Service
public class FriendServiceImpl extends ServiceImpl<FriendMapper, Friend> implements FriendService {
    @Resource
    private UserGiftService userGiftService;
    @Resource
    private GiftMapper giftMapper;

    @Override
    public void becomeFriend(Friend friend) {
        //判断礼物是否合法
        Gift gift = giftMapper.selectById(friend.getGiftId());
        if (!"3".equals(gift.getType())){
            throw new ErrorCodeException("礼物错误");
        }

        //男用户向女用户送礼
        UserGift userGift = new UserGift();
        userGift.setGiftId(friend.getGiftId());
        userGift.setSendUserId(friend.getManId());
        userGift.setUserId(friend.getWomanId());
        userGiftService.sendGift(userGift);

        //成为密聊好友
        friend.setDeadline(DateUtil.changeDateByMonth(new Date(),1));
        save(friend);
    }
}
